1
|
|
|
'use strict'; |
2
|
|
|
|
3
|
|
|
import { assert } from 'chai'; |
4
|
|
|
import { Input, Lexer, Token } from '../../src/Lexer'; |
5
|
|
|
import reservedKeywords from '../../lang/ReservedKeywords.json'; |
6
|
|
|
import pkg from '../../package.json'; |
7
|
|
|
|
8
|
|
|
/** @test {Lexer} */ |
9
|
|
|
describe(`${pkg.name}/Lexer/Lexer`, () => { |
10
|
|
|
/** @test {Lexer#constructor} */ |
11
|
|
|
describe('#constructor', () => { |
12
|
|
|
it('Create a new instance of type Lexer', () => { |
13
|
|
|
const input = new Input('+ hello botlang'); |
14
|
|
|
|
15
|
|
|
assert.instanceOf( |
16
|
|
|
new Lexer(input), Lexer |
17
|
|
|
); |
18
|
|
|
}); |
19
|
|
|
}); |
20
|
|
|
|
21
|
|
|
/** @test {Lexer#next} */ |
22
|
|
|
describe('#next', () => { |
23
|
|
|
it('Ignore comments', () => { |
24
|
|
|
const input = new Input('# A comment'), |
25
|
|
|
token = new Lexer(input).next(); |
26
|
|
|
|
27
|
|
|
assert.isNull(token); |
28
|
|
|
}); |
29
|
|
|
|
30
|
|
|
it('Return string token', () => { |
31
|
|
|
const input = new Input('"Hello World"'), |
32
|
|
|
token = new Lexer(input).next(); |
33
|
|
|
|
34
|
|
|
assert.instanceOf(token, Token); |
35
|
|
|
assert.strictEqual(token.getType(), 'string'); |
36
|
|
|
assert.strictEqual(token.getValue(), 'Hello World'); |
37
|
|
|
}); |
38
|
|
|
|
39
|
|
|
it('Return numeric token', () => { |
40
|
|
|
const tests = [ |
41
|
|
|
1, 1.2, 1234567890, 1234567.890 |
42
|
|
|
]; |
43
|
|
|
|
44
|
|
|
tests.forEach((test) => { |
45
|
|
|
const input = new Input(test), |
46
|
|
|
token = new Lexer(input).next(); |
47
|
|
|
|
48
|
|
|
assert.instanceOf(token, Token); |
49
|
|
|
assert.strictEqual(token.getType(), 'numeric'); |
50
|
|
|
assert.strictEqual(token.getValue(), test); |
51
|
|
|
}); |
52
|
|
|
}); |
53
|
|
|
|
54
|
|
|
it('Return operation token', () => { |
55
|
|
|
const tests = [ |
56
|
|
|
'+', |
57
|
|
|
'-', |
58
|
|
|
'*', |
59
|
|
|
'/', |
60
|
|
|
'%', |
61
|
|
|
'=', |
62
|
|
|
'&', |
63
|
|
|
'|', |
64
|
|
|
'<', |
65
|
|
|
'>', |
66
|
|
|
'!' |
67
|
|
|
]; |
68
|
|
|
|
69
|
|
|
tests.forEach((test) => { |
70
|
|
|
const input = new Input(test), |
71
|
|
|
token = new Lexer(input).next(); |
72
|
|
|
|
73
|
|
|
assert.instanceOf(token, Token); |
74
|
|
|
assert.strictEqual(token.getType(), 'operation'); |
75
|
|
|
assert.strictEqual(token.getValue(), test); |
76
|
|
|
}); |
77
|
|
|
}); |
78
|
|
|
|
79
|
|
|
it('Return punctuator token', () => { |
80
|
|
|
const tests = [ |
81
|
|
|
',', |
82
|
|
|
';', |
83
|
|
|
'(', |
84
|
|
|
')', |
85
|
|
|
'{', |
86
|
|
|
'}', |
87
|
|
|
'[', |
88
|
|
|
']' |
89
|
|
|
]; |
90
|
|
|
|
91
|
|
|
tests.forEach((test) => { |
92
|
|
|
const input = new Input(test), |
93
|
|
|
token = new Lexer(input).next(); |
94
|
|
|
|
95
|
|
|
assert.instanceOf(token, Token); |
96
|
|
|
assert.strictEqual(token.getType(), 'punctuator'); |
97
|
|
|
assert.strictEqual(token.getValue(), test); |
98
|
|
|
}); |
99
|
|
|
}); |
100
|
|
|
|
101
|
|
|
it('Return identifier token', () => { |
102
|
|
|
const tests = [ |
103
|
|
|
'functionName' |
104
|
|
|
]; |
105
|
|
|
|
106
|
|
|
tests.forEach((test) => { |
107
|
|
|
const input = new Input(test), |
108
|
|
|
token = new Lexer(input).next(); |
109
|
|
|
|
110
|
|
|
assert.instanceOf(token, Token); |
111
|
|
|
assert.strictEqual(token.getType(), 'identifier'); |
112
|
|
|
assert.strictEqual(token.getValue(), test); |
113
|
|
|
}); |
114
|
|
|
}); |
115
|
|
|
|
116
|
|
|
it('Return keyword token', () => { |
117
|
|
|
reservedKeywords.forEach((test) => { |
118
|
|
|
const input = new Input(test), |
119
|
|
|
token = new Lexer(input).next(); |
120
|
|
|
|
121
|
|
|
assert.instanceOf(token, Token); |
122
|
|
|
assert.strictEqual(token.getType(), 'keyword'); |
123
|
|
|
assert.strictEqual(token.getValue(), test); |
124
|
|
|
}); |
125
|
|
|
}); |
126
|
|
|
|
127
|
|
|
it('Throw invalid character error', () => { |
128
|
|
|
const input = new Input('~'), |
129
|
|
|
lexer = new Lexer(input); |
130
|
|
|
|
131
|
|
|
assert.throws(() => lexer.next(), Error, 'Invalid character (Line: 1, Column: 0)'); |
132
|
|
|
}); |
133
|
|
|
}); |
134
|
|
|
|
135
|
|
|
/** @test {Lexer#peek} */ |
136
|
|
|
describe('#peek', () => { |
137
|
|
|
it('Return the current token.', () => { |
138
|
|
|
const input = new Input('"hello botlang"'), |
139
|
|
|
lexer = new Lexer(input); |
140
|
|
|
|
141
|
|
|
assert.instanceOf( |
142
|
|
|
lexer.peek(), Token |
143
|
|
|
); |
144
|
|
|
}); |
145
|
|
|
}); |
146
|
|
|
}); |
147
|
|
|
|